package com.item.schedule.controller;

import com.item.entity.ScheduleJobEntity;
import com.item.result.ResultVo;
import com.item.schedule.service.ScheduleJobService;
import com.item.util.PageUtilsPlus;
import com.item.util.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * 定时任务
 *
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 分页查询定时任务
     *
     * @param params 查询参数
     * @return R
     */
    @GetMapping("/list")
    //@RequiresPermissions("sys:schedule:list")
    public ResultVo list(@RequestParam Map<String, Object> params) {
        PageUtilsPlus pageUtil = scheduleJobService.queryPage(params);
        return ResultVoUtil.success(pageUtil);
    }

    /**
     * 根据主键查询详情
     *
     * @param jobId 主键
     * @return R
     */
    @GetMapping("/info/{jobId}")
    //@RequiresPermissions("sys:schedule:info")
    public ResultVo info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.getById(jobId);
        return ResultVoUtil.success(schedule);
    }

    /**
     * 新增定时任务
     *
     * @param scheduleJob scheduleJob
     * @return R
     */
    @PostMapping("/save")
    //@RequiresPermissions("sys:schedule:save")
    public ResultVo save(@Valid @RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.add(scheduleJob);
        return ResultVoUtil.success();
    }

    /**
     * 修改定时任务
     *
     * @param scheduleJob scheduleJob
     * @return R
     */
    @PostMapping("/update")
    //@RequiresPermissions("sys:schedule:update")
    public ResultVo update(@Valid @RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.update(scheduleJob);
        return ResultVoUtil.success();
    }

    /**
     * 删除定时任务
     *
     * @param jobIds jobIds
     * @return R
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sys:schedule:delete")
    public ResultVo delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return ResultVoUtil.success();
    }

    /**
     * 立即执行任务
     *
     * @param
     * @return R
     */
    @PostMapping("/run")
    //@RequiresPermissions("sys:schedule:run")
    public ResultVo run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return ResultVoUtil.success();
    }

    /**
     * 暂停定时任务
     *
     * @param jobIds jobIds
     * @return R
     */
    @PostMapping("/pause")
    //@RequiresPermissions("sys:schedule:pause")
    public ResultVo pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);

        return ResultVoUtil.success();
    }

    /**
     * 恢复定时任务
     *
     * @param jobIds jobIds
     * @return R
     */
    @PostMapping("/resume")
    //@RequiresPermissions("sys:schedule:resume")
    public ResultVo resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return ResultVoUtil.success();
    }

}
